package com.mvc.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2014年3月7日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Controller
public class FileDownController
{
	
	private static final String UPLOADDIR = "/uploadDir/";  

    @RequestMapping(value="/down",method = RequestMethod.GET)  
    public ResponseEntity<byte[]> download() throws IOException {  
        String path="E:/test/test.zip";
        System.out.println("SSSSSSSSS        "+path);
        File file=new File(path);
        HttpHeaders headers = new HttpHeaders();  
        String fileName=new String("中文.zip".getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName); 
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); 
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);  
    }  


    /** 
     * 下载 
     *  
     * @author geloin 
     * @date 2012-3-29 下午5:24:14 
     * @param attachment 
     * @param request 
     * @param response 
     * @return 
     * @throws Exception 
     */  
    @RequestMapping(value = "/download/{instId}/{date}/{fileName}",method = RequestMethod.GET)  
    public ModelAndView fileDownload(HttpServletRequest request, 
            HttpServletResponse response, @PathVariable("instId") String instId,@PathVariable("date") String date,@PathVariable("fileName") String fileName) throws Exception {  
  
    	System.out.println(date + "==========" + fileName);
    	
    	String certId = request.getParameter("certId");
    	String sign = request.getParameter("sign");
    	
    	System.out.println(certId + "==========" + sign);
    	
        String storeName = "test.zip";  
        String realName = "Java设计模式.zip";  
        String contentType = "application/octet-stream";  
        download(request, response, storeName, contentType,realName);  
        return null;  
    }  

    /**
     * 下载文件具体方法
     * @param request
     * @param response
     * @param storeName
     * @param contentType
     * @param realName
     * @throws Exception
     */
	public static void download(HttpServletRequest request,HttpServletResponse response, String storeName,
			String contentType,String realName) throws Exception {  
	    response.setContentType("text/html;charset=UTF-8");  
	    request.setCharacterEncoding("UTF-8");  
	    BufferedInputStream bis = null;  
	    BufferedOutputStream bos = null;  
	
	    String ctxPath = UPLOADDIR;  
	    ctxPath ="E:/test/";
	    String downLoadPath = ctxPath + storeName;  
	
	    long fileLength = new File(downLoadPath).length();  
	
	    response.setContentType(contentType);  
	    response.setHeader("Content-disposition", "attachment; filename=" + new String(realName.getBytes("utf-8"), "ISO8859-1"));  
	    response.setHeader("Content-Length", String.valueOf(fileLength));  
	    try{
	    	bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
	 	    bos = new BufferedOutputStream(response.getOutputStream());  
	 	    byte[] buff = new byte[2048];  
	 	    int bytesRead;  
	 	    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
	 	        bos.write(buff, 0, bytesRead);  
	 	    } 
	 	    bos.flush();
	    }catch(Exception e){
	    	throw e;
	    }finally{
	    	if(bis!=null){
	    		bis.close();
	    	}
	    	if(bos!=null){
	    		bos.close(); 
	    	}
	    }
	}  
} 

